package com.zmn.mcc.admin.controller.dept;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.business.interfaces.staff.StaffBService;
import com.zmn.mcc.model.entity.dept.McDept;
import com.zmn.mcc.model.entity.dept.McDeptQuery;
import com.zmn.mcc.model.entity.organize.McOrganize;
import com.zmn.mcc.model.entity.role.McRole;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.services.interfaces.role.RoleService;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author heguanghua
 * @since 2020/04/15 10:28
 */
@Controller
@RequestMapping("/dept/")
public class DeptListController extends DeptBaseController {

    @Resource
    private RoleService roleService;
    @Resource
    private StaffBService staffBService;
    @Resource
    private StaffService staffService;


    /**
     * 部门列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @ModelAttribute McDeptQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setDeptId(Integer.parseInt(keyword));
            query.setDeptName("");
        } else {
            query.setDeptName(keyword);
        }

        List<McDept> items = deptService.listPageByQuery(query);
        McDept parent = !NumberUtil.isNullOrZero(query.getParentId()) ? deptService.findByKey(query.getParentId()) : new McDept();

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("parent", parent);
        model.addAttribute("keyword", keyword);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/list";
    }

    /**
     * 部门树状列表
     */
    @RequestMapping("tree")
    public String config(HttpServletRequest request,
                         @ModelAttribute McDeptQuery query,
                         Model model) {

        McOrganize organize = NumberUtil.isNotNullOrZero(query.getOrgId()) ? organizeService.findByKey(query.getOrgId()) : new McOrganize();

        model.addAttribute("organize", organize);
        model.addAttribute("vo", query);

        return "dept/list_tree";
    }

    @RequestMapping("ajaxTree")
    @ResponseBody
    public ResponseDTO ajaxTree(HttpServletRequest request, HttpServletResponse response,
                                @ModelAttribute McDeptQuery query) {

        List<McDept> items = deptBService.listDept(query);

        if (CollectionUtil.isNotNullOrEmpty(items)) {
            return ResponseDTO.success(items, "OK");
        } else {
            return ResponseDTO.success(Collections.emptyList(), "部门架构列表为空");
        }
    }

    /**
     * 部门详情
     */
    @RequestMapping("detail")
    public String detail(HttpServletRequest request,
                         @RequestParam(value = "orgId", defaultValue = "0") Integer orgId,
                         @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                         @RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
                         Model model) {

        McDept obj = !NumberUtil.isNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McDept parent = new McDept();
        McOrganize organize = new McOrganize();
        McRole role = new McRole();
        McStaff leader = new McStaff();
        McStaff manager = new McStaff();
        if (NumberUtil.isNullOrZero(deptId) && NumberUtil.isNotNullOrZero(parentId)) {
            obj.setParentId(parentId);
        }
        if (obj.getParentId() != null && obj.getParentId() > GlobalConsts.TOP_ID) {
            parent = Optional.ofNullable(deptService.findByKey(obj.getParentId())).orElse(new McDept());
            obj.setOrgId(parent.getOrgId());
        }
        if (NumberUtil.isNullOrZero(obj.getOrgId()) && NumberUtil.isNotNullOrZero(orgId)) {
            obj.setOrgId(orgId);
        }
        if (NumberUtil.isNotNullOrZero(obj.getOrgId())) {
            organize = organizeService.findByKey(obj.getOrgId());
        }
        if (NumberUtil.isNotNullOrZero(obj.getRoleId())) {
            role = roleService.findByKey(obj.getRoleId());
        }
        if (NumberUtil.isNotNullOrZero(obj.getLeaderId())) {
            leader = staffService.findByKey(obj.getLeaderId());
        }
        if (NumberUtil.isNotNullOrZero(obj.getManagerId())) {
            manager = staffService.findByKey(obj.getManagerId());
        }

        List<McDept> treeDept = deptBService.buildTreeDept(organize.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("parent", parent);
        model.addAttribute("organize", organize);

        model.addAttribute("role", role);
        model.addAttribute("leader", leader);
        model.addAttribute("manager", manager);
        model.addAttribute("parentList", treeDept);

        if (NumberUtil.isNotNullOrZero(deptId)) {
            model.addAttribute("layers", deptBService.listDeptForAbove(deptId, true));
        }

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/detail";
    }

    /**
     * 跳转绑定北森页面
     *
     * @param deptId 部门ID
     */
    @RequestMapping(value = "toSetBeisenId")
    public ModelAndView toSetBeisenId(@RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {
        McDept dept = deptId > 0 ? deptService.findByKey(deptId) : new McDept();

        ModelAndView modelAndView = new ModelAndView("dept/setBeisenDeptId");
        modelAndView.addObject("dept", dept);

        return modelAndView;
    }
}
